<template>
  <el-dialog :title="title()" :visible.sync="visible" width="66%" @open="onOpen" :before-close="onCancel">
    <el-form :model="dataAccount">
      <el-form-item label="phone" :label-width="formLabelWidth">
        <el-input v-model="dataAccount.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="password" :label-width="formLabelWidth">
        <el-input v-model="dataAccount.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="proxy" :label-width="formLabelWidth">
        <el-input v-model="dataAccount.proxy" autocomplete="off" placeholder="格式参考 socks5://ip:port:username:password 或者 http://ip:port:username:password"></el-input>
      </el-form-item>
      <el-form-item label="wallet_address" :label-width="formLabelWidth">
        <el-input v-model="dataAccount.wallet_address" autocomplete="off" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="API Account?" :label-width="formLabelWidth">
        <el-radio-group v-model="dataAccount.api">
          <el-radio label="yes">API</el-radio>
          <el-radio label="no">Normal</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onCancel">取 消</el-button>
      <el-button type="primary" @click="onConfirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  props: {
    account: { type: Object, default: null },
    visible: { type: Boolean, default: false },
  },
  data() {
    return {
      formLabelWidth: '120px',
      dataAccount: {}
    }
  },
  methods: {
    onCancel() {
      this.$emit("on-dismiss")
    },
    async onConfirm() {
      if (this.dataAccount.api == null) {
        this.dataAccount.api = 'no'
      }
      this.dataAccount.wallet_address = (this.dataAccount.wallet_address || "").trim()
      if ( this.dataAccount.wallet_address != '' && this.dataAccount.wallet_address != null) {
        console.log(this.dataAccount.wallet_address)
        // caculate user friendly wallet address for this client
        
      } else {
        console.log('wallet address is null or  blank ')
      }

      if (this.dataAccount.id == null) {
        await this.$store.dispatch("addAccount", this.dataAccount)
      } else {
        await this.$store.dispatch("updateAccount", this.dataAccount)
      }
      this.$emit("on-dismiss")
    },
    onOpen() {
      this.dataAccount = {
        ...(this.account ?? {}),
      }
    },
    title() {
      if (this.account == null) {
        return ""
      }
      if (this.account.id == null) {
        return "Add Account"
      } else {
        return "Update Account"
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.version-update {
  &-info {
    margin-left: 10px;
    margin-top: 10px;
    justify-content: flex-start;
    flex-direction: column;
    align-items: flex-start;
  }

  &-footer {
    margin-right: 60px;
  }
}
</style>